<?php

namespace app\common\service;

use app\common\model\Admin;
use app\common\model\AdminRole as Model;
use app\common\util\AdminPower;
use app\common\util\LogHelper;
use app\common\util\WhereBuilder;
use think\facade\Validate;

/**
 * 管理员角色 业务类
 */
class AdminRoleService extends Service
{
    protected string $model_name = 'role';
    protected string $model_cname = '管理员角色';

    /**
     * 列表
     */
    public function list($param = [])
    {
        $param = $this->autoParams($param);

        $keyword = $this->pg('keyword');

        $where = WhereBuilder::builder()
            ->like('name', $keyword)
            ->build();

        $list = (new Model)->where($where)->order('sort desc, id desc')->select();
        foreach ($list as $k => $v) {
            $list[$k]['value'] = $v->getAttr('id');
            $list[$k]['label'] = $v->getAttr('name');
        };
        return $list;
    }

    /**
     * 管理员分页
     */
    public function adminPage($param = [])
    {
        $param = $this->autoParams($param);

        $keyword = $this->pg('keyword');

        $where = WhereBuilder::builder()
            ->like('name', $keyword)
            ->build();

        return (new Model)->where($where)->order('id desc')->paginate($this->tp6Page());
    }

    /**
     * 创建
     * @param $param
     * @return Model|\think\Model
     */
    public function create($param = [])
    {
        $param = $this->autoParams($param);
        $data = $param[$this->model_name];

        $this->validate($data, Validate::rule([
            'name|名称' => 'require',
            'codes|权限' => 'require',
            'sort|排序' => 'require',
            //'valid|有效性' => 'require',
        ]));

        $res = Model::create($data);

        //日志记录
        LogHelper::logAdmin($res, '创建' . $this->model_cname);
        return $res;
    }

    /**
     * 更新
     * @param $param
     * @return Model
     * @throws \Exception
     */
    public function update($param = [])
    {
        $param = $this->autoParams($param);
        $data = $param[$this->model_name];

        $this->validate($data, Validate::rule([
            'name|名称' => 'require',
            'codes|权限' => 'require',
            'sort|排序' => 'require',
            //'valid|有效性' => 'require',
        ]));

        // 检查是否存在
        $model = Model::findOrEmpty($data['id']);
        if ($model->isEmpty()) {
            throw $this->exception($this->model_cname . '不存在');
        }

        //更新
        $model->save($data);

        //日志记录
        LogHelper::logAdmin($model, '更新' . $this->model_cname);

        return $model;
    }

    /**
     * 删除
     * @param $param
     * @return Model|mixed
     * @throws \Exception
     */
    public function delete($param = [])
    {
        $this->autoParams($param);
        $model = $this->one(Model::class);

        if (Admin::where('role_id', $model->id)->find()) {
            throw $this->exception('已有管理员关联该' . $this->model_cname . '，不能删除');
        }

        $model->delete();

        //日志记录
        LogHelper::logAdmin($model, '删除' . $this->model_cname);

        return $model;
    }

    /**
     * 获取权限
     * @param $param
     * @return array|\string[][]
     */
    public function getPower($param = [])
    {
        $this->autoParams($param);
        return AdminPower::getPowerAssign();
    }
}
